package com.wx_shop.api.util;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class FastJsonConvert {
	private static final Logger LOGGER = LoggerFactory.getLogger(FastJsonConvert.class);

	private static final SerializerFeature[] featuresWithNullValue = { SerializerFeature.WriteMapNullValue,
			SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.WriteNullListAsEmpty,
			SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullStringAsEmpty };

	private static final SerializerFeature[] featuresWithPrettyFormat = { SerializerFeature.PrettyFormat };

	public static <T> T convertJSONToObject(String data, Class<T> clzss) {
		try {
			T t = JSON.parseObject(data, clzss);
			return t;
		} catch (Exception e) {
			LOGGER.error("fastjson转换异常：", e);
		}
		return null;
	}

	public static <T> List<T> convertJSONToArray(String data, Class<T> clzss) {
		try {
			List<T> t = JSON.parseArray(data, clzss);
			return t;
		} catch (Exception e) {
			LOGGER.error("fastjson转换异常：", e);
		}
		return null;
	}

	public static String convertObjectToJSON(Object obj) {
		try {
			String text = JSON.toJSONString(obj);
			return text;
		} catch (Exception e) {
			LOGGER.error("fastjson转换异常：", e);
		}
		return null;
	}

	public static String convertObjectToJsonWithPrettyFormat(Object obj) {
		try {
			String text = JSON.toJSONString(obj, featuresWithPrettyFormat);
			return text;
		} catch (Exception e) {
			LOGGER.error("fastjson转换异常：", e);
		}
		return null;
	}

	public static String convertObjectToJSONWithNullValue(Object obj) {
		try {
			String text = JSON.toJSONString(obj, featuresWithNullValue);
			return text;
		} catch (Exception e) {
			LOGGER.error("fastjson转换异常：", e);
		}
		return null;
	}

	public static void main(String[] args) {
		System.err.println(System.getProperties());
	}
}